package it.univaq.disim.mwt.business.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import it.univaq.disim.mwt.business.EventoService;
import it.univaq.disim.mwt.business.model.Evento;
import it.univaq.disim.mwt.business.model.Insegnante;
import it.univaq.disim.mwt.business.model.PersonaleAta;
import it.univaq.disim.mwt.business.model.TipologiaLaboratorio;

public class JDBCEventoService implements EventoService {

	@Override
	public Evento findEventoById(Long id) {
		// TODO Auto-generated method stub
		 Connection connection = null;
			PreparedStatement preparedStatement = null;
			ResultSet resultSet = null;
			
			String sql="select * from eventi where id = ? ";
			try {
				connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "school", "school");
				preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setLong(1, id);
				preparedStatement.executeUpdate();	
			    resultSet = preparedStatement.getResultSet();
			    resultSet.next();
			    
			    Long ide=resultSet.getLong("id");
			    String nome=resultSet.getString("nome");
			    String tipo=resultSet.getString("tipo");
			    String aula=resultSet.getString("aula");
			    Long idResponsabile=resultSet.getLong("id_resposabile_professore");
			    String descrizione=resultSet.getString("descrizione");
			    Long idResponsabileAta=resultSet.getLong("id_responsabile_ata");
			    Date data=resultSet.getDate("data");
			    
			    JDBCInsegnanteService jdbcInsegnanteService= new JDBCInsegnanteService();
			    Insegnante insegnante=jdbcInsegnanteService.findInsegnanteById(idResponsabile);
			    
			    JDBCPersonaleAtaService jdbcPersonaleAtaService=new JDBCPersonaleAtaService();
			    PersonaleAta personaleAta=jdbcPersonaleAtaService.findPersonaleAtaById(idResponsabileAta);
			    
			    
			    Evento evento= new Evento(nome, tipo, aula, insegnante, descrizione, personaleAta, data);
			    
			    resultSet.close();
				preparedStatement.close();
				connection.close();
				
				return evento ;
			
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
			
		return null;
	}

	@Override
	public List<Evento> findAllEventi() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		List<Evento> list = new ArrayList<Evento>();
		String sql = " select * from eventi ";
		
		try {
			connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "school", "school");
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.executeQuery();	
			resultSet=preparedStatement.getResultSet();
			
			while(resultSet.next()){
				Long ide=resultSet.getLong("id");
			    String nome=resultSet.getString("nome");
			    String tipo=resultSet.getString("tipo");
			    String aula=resultSet.getString("aula");
			    Long idResponsabile=resultSet.getLong("id_resposabile_professore");
			    String descrizione=resultSet.getString("descrizione");
			    Long idResponsabileAta=resultSet.getLong("id_responsabile_ata");
			    Date data=resultSet.getDate("data");
			    
			    JDBCInsegnanteService jdbcInsegnanteService= new JDBCInsegnanteService();
			    Insegnante insegnante=jdbcInsegnanteService.findInsegnanteById(idResponsabile);
			    
			    JDBCPersonaleAtaService jdbcPersonaleAtaService=new JDBCPersonaleAtaService();
			    PersonaleAta personaleAta=jdbcPersonaleAtaService.findPersonaleAtaById(idResponsabileAta);
			    
			    
			    Evento evento= new Evento(nome, tipo, aula, insegnante, descrizione, personaleAta, data);
			    
	            
	            list.add(evento);
			}
			
			resultSet.close();
			preparedStatement.close();
			connection.close();
		
			return list;
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	

}
